<?php
namespace Models\Model;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
use Zend\Db\Sql;
use Zend\Db\Sql\Where;
use Zend\Db\Sql\Select;
use Zend\Paginator\Adapter\DbSelect;
use Zend\Paginator\Paginator;
use Zend\Db\Sql\Predicate;
use Zend\Db\Sql\Expression;
class UserdetailesTable
{
    protected $tableGateway;
	protected $select;
    public function __construct(TableGateway $tableGateway)
    {
        $this->tableGateway = $tableGateway;
		$this->select = new Select();
    }
	//Registration
	public function registerDetailes($register){
		$data = array(
			'imei_number' 				=> $register['imei_number'],'lifetime_wins'  	 		=> 0,  	
			'numberof_coins' 	 		=> 1000,  
			'chance_per_day'	 		=> 0, 				
			'numberof_failure_attempts'	=> 0, 				
			'success_per_day'	 		=> 0, 				
			'date'	 					=> date('Y-m-d H:i:s'), 				
		);	
		$insertresult=$this->tableGateway->insert($data);	
		return $this->tableGateway->lastInsertValue;
    }	
	//Check imei number
	
	public function checkImeiNumber($imei_number){	
		$select = $this->tableGateway->getSql()->select();
		$select->where('imei_number="'.$imei_number.'"');	
		$resultSet = $this->tableGateway->selectWith($select);
		return $resultSet;
	}
	
	//update Imei number
	public function updateUserDetailes($imei_number){	
		$data = array(
			'date'          => date('Y-m-d H:i:s'),
		);
	   $updateuserid=$this->tableGateway->update($data, array('imei_number' => $imei_number));
	   return $updateuserid;                        
	}
	//Update Failed
	
	public function updateFailed($updateData,$imei_number,$chance_per_day){	
		$data = array(
			'numberof_coins' 	 		=> $updateData[0]['numberof_coins']-1,  
			'chance_per_day'	 		=> $chance_per_day+1, 				
			'numberof_failure_attempts'	=> $updateData[0]['numberof_failure_attempts']+1, 
			 
		);
	   $updateuserid=$this->tableGateway->update($data, array('imei_number' => $imei_number));
	   return $updateuserid;                        
	}
	
	//Update Success
	
	public function updateSuccess($updateData,$imei_number,$chance_per_day,$success_per_day){	
		$data = array(
			'numberof_coins' 	 		=> $updateData[0]['numberof_coins']-1,  
			'chance_per_day'	 		=> $chance_per_day+1, 				
			'lifetime_wins'  	 		=> $updateData[0]['lifetime_wins']+1,
			'success_per_day'			=> $success_per_day+1,
			'numberof_failure_attempts'	=> 0,
		);
	   $updateuserid=$this->tableGateway->update($data, array('imei_number' => $imei_number));
	   return $updateuserid;                        
	}
	//Check Uc statsus
	
	public function checkSCDStatus($data){	//echo '<pre>'; print_r($data);exit;
		$select = $this->tableGateway->getSql()->select();
		$select->where('imei_number="'.$data[0].'"');	
		$select->where('success_per_day < "'.$data[1].'"');	
		$resultSet = $this->tableGateway->selectWith($select);
		return $resultSet;
	}
	//Change uc status
	public function changeCPDStatus($imei_number){	
		$data = array(
			'chance_per_day'	 		=> 0, 	
		);
	   $updateuserid=$this->tableGateway->update($data, array('imei_number' => $imei_number));
	   return $updateuserid;                        
	}
}